'use strict';

const Op = require('sequelize').Op;
const path = require('path');

module.exports = appInfo => {

  const config = exports = {};
  config.rundir = process.cwd() + '/run';
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1707394383972_7725';
  config.logger = {
    dir: path.join(process.cwd(), 'logs'),
  };
  config.cluster = {
    // 禁用 Agent 进程的调试端口
    inspectPort: false,
    // 确保 Worker 进程也不启用调试
    workers: process.env.NODE_ENV === 'production' ? 2 : 1,
    // 可选：设置日志级别，减少调试信息干扰
    loggerLevel: 'INFO',
  };

  // 中间件配置
  config.middleware = [ 'errorHandler', 'responseHandler' ];
  // 安全配置
  config.security = {
    csrf: {
      enable: false,
    },
    domainWhiteList: [ '*' ],
  };

  // 跨域配置
  config.cors = {
    origin: '*',
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS',
  };

  // 数据库配置
  config.sequelize = {
    // 使用默认运算符别名
    operatorsAliases:{
      $eq: Op.eq,
      $ne: Op.ne,
      $gte: Op.gte,
      $gt: Op.gt,
      $lte: Op.lte,
      $lt: Op.lt,
      $not: Op.not,
      $in: Op.in,
      $notIn: Op.notIn,
      $is: Op.is,
      $like: Op.like,
      $notLike: Op.notLike,
      $iLike: Op.iLike,
      $notILike: Op.notILike,
      $regexp: Op.regexp,
      $notRegexp: Op.notRegexp,
      $iRegexp: Op.iRegexp,
      $notIRegexp: Op.notIRegexp,
      $between: Op.between,
      $notBetween: Op.notBetween,
      $overlap: Op.overlap,
      $contains: Op.contains,
      $contained: Op.contained,
      $adjacent: Op.adjacent,
      $strictLeft: Op.strictLeft,
      $strictRight: Op.strictRight,
      $noExtendRight: Op.noExtendRight,
      $noExtendLeft: Op.noExtendLeft,
      $and: Op.and,
      $or: Op.or,
      $any: Op.any,
      $all: Op.all,
      $values: Op.values,
      $col: Op.col
    },
    dialect: 'mysql',
    host: '118.190.150.72',
    port: 3306,
    database: 'cloud_door_permit',
    username: 'root',
    password: 'zzq199905',
    timezone: '+08:00',
    define: {
      timestamps: true,
      underscored: true,
      freezeTableName: true,
    },
  };

  // JWT配置
  config.jwt = {
    secret: 'cloud-door-permit-jwt-secret',
    enable: true,
    match: /^\/api\/(?!admin\/login)/,
  };

  // 文件上传配置
  config.multipart = {
    mode: 'file',
    fileSize: '10mb',
    whitelist: [
      '.jpg', '.jpeg', '.png', '.gif', '.webp',
    ],
  };

  // 静态文件配置
  config.static = {
    prefix: '/public/',
    dir: process.cwd() + '/app/public',
    dynamic: true,
    preload: false,
    maxAge: 31536000,
    buffer: true,
  };

  // 上传目录配置
  config.upload = {
    baseDir: path.join(process.cwd(), 'app/public/uploads'),
    buckets: [ 'users', 'permits', 'announcements', 'default' ],
  };

  // 错误处理配置
  config.onerror = {
    all(err, ctx) {
      // 在此处定义针对所有响应类型的错误处理方法
      ctx.body = {
        code: err.status || 500,
        message: err.message,
        data: null,
      };
      ctx.status = err.status || 500;
    },
  };

  // 参数校验配置
  config.validate = {
    convert: true,
    widelyUndefined: true,
  };

  config.weapp = {
    appId: 'your_appid', // 小程序 appId
    appSecret: 'your_appsecret', // 小程序 appSecret
    // 订阅消息模板ID
    templates: {
      applyNotify: 'template_id_1',      // 申请提醒通知
      auditResultNotify: 'template_id_2', // 审核结果通知
      verifyResultNotify: 'template_id_3' // 核验结果通知
    }
  };

  return {
    ...config,
  };
};
